﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using WebComputers.Class;
using System.Web.Services;


namespace WebComputers
{
    public partial class DanhMucSanPham : System.Web.UI.Page
    {
        const int slHienThi = 9;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                if (Request.QueryString.Count > 0)
                {
                    int totalRows = Convert.ToInt32(hdfPageTotal.Value);
                    if (DAHelper.CheckNumber(Request.QueryString["id"]))
                    {
                        int id = Convert.ToInt32(Request.QueryString["id"]);
                        var lhh = LoaiHangHoaDAO.SelectById(id);
                        lblTenLoai.Text = lhh.sTenLoai;
                        rptDanhMucHangHoa.DataSource = DanhMucHangHoaDAO.SelectByLoaiSanPhamLinq(id, 1, slHienThi, ref totalRows);
                        rptDanhMucHangHoa.DataBind();
                    }
                    if (string.IsNullOrEmpty(lblTenLoai.Text) || rptDanhMucHangHoa.Items.Count == 0)
                    {
                        lblTenLoai.Text = "Danh mục sản phẩm rỗng!";
                    }
                    else
                    {
                        hdfPageTotal.Value = DAHelper.CalculateTotalPages(totalRows, slHienThi).ToString();
                        hdfLoaiSanPham_ID.Value = Request.QueryString["id"].ToString();
                    }
                }
                else
                {
                    Response.Redirect("SanPham.aspx", false);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        protected string FillImage(string fileName)
        {
            return DAHelper.FillImage(fileName);
        }

        protected void rptDanhMucHangHoa_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            
        }

        public void RefreshCart()
        {
            UpdatePanel up = Master.FindControl("upGioHang") as UpdatePanel;
            up.Update();
        }

        [WebMethod(EnableSession=true)]
        public static string GetProduct(int pageIndex, int loaiSanPham_ID)
        {
            
            int totalRows = 0;
            //IEnumerable<DanhMucSanPham> rs = DanhMucHangHoaDAO.SelectByLoaiSanPhamLinq(loaiSanPham_ID, pageIndex, slHienThi, ref totalRows) as IEnumerable<DanhMucSanPham>;
            //DAHelper.ToDataTable(rs);
            //This is extension method
            //rs.ToDataTable();            
            //DataTable dt = DAHelper.ToDataTable<DanhMucHangHoa>(rs);
            //DataSet ds = new DataSet();
            //ds.Tables.Add(dt);
            //string returnStr = ds.GetXml();
            //DanhMucSanPham dmsp = new DanhMucSanPham();
            //dmsp.RefreshCart();
            string returnStr = DAHelper.ConvertToXml(DanhMucHangHoaDAO.SelectByLoaiSanPhamLinq(loaiSanPham_ID, pageIndex, slHienThi, ref totalRows));
            return returnStr;
        }

        [WebMethod(EnableSession = true)]
        public static string AddToCart(int id)
        {
            return DAHelper.AddToCart(id);
        }
    }
}